package jp.dodododo.reloadable.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		if (args == null || args.length == 0) {
			showHelp();
			return;
		}
		File file = new File(args[0]);
		FileInputStream is = new FileInputStream(file);
		BufferedReader reader = null;
		try {
			String encoding = args[1];
			reader = new BufferedReader(new InputStreamReader(is, encoding));
		} catch (RuntimeException ignore) {
			reader = new BufferedReader(new InputStreamReader(is));
		}
		while (true) {
			String line = reader.readLine();
			if (line == null) {
				break;
			}

			if (line.contains("<filter-class>")) {
				processFilterClass(line);
			} else if (line.contains("<servlet-class>")) {
				processServletClass(line);
			} else {
				System.out.println(line);
			}
		}
		System.out.println(getAddTags());
	}

	private static void processServletClass(String line) {
		int firstCharIndex = line.indexOf("<servlet-class>");
		int start = firstCharIndex + "<servlet-class>".length();
		int end = line.indexOf("</servlet-class>");
		String targetClassName = line.substring(start, end);
		String indent = line.substring(0, firstCharIndex);

		System.out.println("<!-- " + line + " -->");
		System.out.println(indent + "<servlet-class>jp.dodododo.reloadable.servlet.impl.ServletWrapperImpl</servlet-class>");
		System.out.println(indent + "<init-param>");
		System.out.println(indent + "  " + "<param-name>jp.dodododo.reloadable.servlet.ServletWrapper.targetClass</param-name>");
		System.out.println(indent + "  " + "<param-value>" + targetClassName + "</param-value>");
		System.out.println(indent + "</init-param>");
	}

	private static void processFilterClass(String line) {
		int firstCharIndex = line.indexOf("<filter-class>");
		int start = firstCharIndex + "<filter-class>".length();
		int end = line.indexOf("</filter-class>");
		String targetClassName = line.substring(start, end);
		String indent = line.substring(0, firstCharIndex);

		System.out.println("<!-- " + line + " -->");
		System.out.println(indent + "<filter-class>jp.dodododo.reloadable.filter.FilterWrapper</filter-class>");
		System.out.println(indent + "<init-param>");
		System.out.println(indent + "  " + "<param-name>jp.dodododo.reloadable.filter.FilterWrapper.targetClass</param-name>");
		System.out.println(indent + "  " + "<param-value>" + targetClassName + "</param-value>");
		System.out.println(indent + "</init-param>");
	}

	private static void showHelp() {
		System.out.println("Usage: java " + Main.class.getName() + " web.xml [encoding]");

	}

	private static String getAddTags() {
		InputStream is = Main.class.getClassLoader().getResourceAsStream("jp/dodododo/reloadable/add_web_xml.txt");
		InputStreamReader reader = new InputStreamReader(is);
		BufferedReader in = new BufferedReader(reader);
		StringBuffer out = new StringBuffer(100);
		try {
			try {
				char[] buf = new char[1024];
				int n;
				while ((n = in.read(buf)) >= 0) {
					out.append(buf, 0, n);
				}
			} finally {
				in.close();
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return out.toString();
	}
}
